﻿Imports System.Data.SqlClient
Imports System.Configuration
Public Class frm_xemdiem
    Sub loadcboLop()
        Dim cmd As New SqlCommand("sp_cboMaLop", conn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim da As New SqlDataAdapter(cmd)
        Dim tb As New DataTable
        da.Fill(tb)
        cbo_malop.DataSource = tb
        cbo_malop.DisplayMember = "TenLop"
        cbo_malop.ValueMember = "TenLop"
    End Sub
    Private Sub frm_xemdiem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadcboLop()
        cbo_malop.Text = "---------CHỌN TÊN LỚP---------"
    End Sub

    Private Sub cmd_xem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_xem.Click

        If txt_mahocvien.Text = "" Then
            MsgBox("Vui lòng nhập mã học viên")
            Exit Sub
        End If
        If cbo_malop.Text = "---------NHẬP TÊN LỚP---------" Or cbo_malop.Text = "" Then
            MsgBox("Vui lòng chọn Tên Lớp")
            Exit Sub
        End If
        Dim cmd As New SqlCommand("sp_XemDiem_select", conn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim da As New SqlDataAdapter(cmd)
        Dim tb As New DataTable
        da.Fill(tb)

        dgv_diem.DataSource = tb
        tb.DefaultView.RowFilter = String.Format("MaHocVien = '{0}' and TenLop= '{1}' ", txt_mahocvien.Text, cbo_malop.Text)

        If dgv_diem.RowCount = 0 Then
            MsgBox("Học viên không học trong lớp này")
            txt_mahocvien.Text = ""
            cbo_malop.Text = "---------NHẬP TÊN LỚP---------"
            dgv_diem.Columns.Clear()
            Exit Sub
        End If
    End Sub
    
   
    Private Sub cmd_PDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_PDF.Click
        If dgv_diem.RowCount = 0 Then
            MsgBox("Chưa có dữ liệu")
        Else
            PrintDocument1.Print()
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim bm As New Bitmap(Me.dgv_diem.Width, Me.dgv_diem.Height)
        dgv_diem.DrawToBitmap(bm, New Rectangle(0, 0, Me.dgv_diem.Width, Me.dgv_diem.Height))
        e.Graphics.DrawString("Bảng điểm học viên có MSSV: " + txt_mahocvien.Text, New Font("tahoma", 15), Brushes.Black, 40, 180)
        e.Graphics.DrawImage(bm, 0, 0)
    End Sub
End Class